﻿using System;
using System.Windows.Controls;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using Microsoft.Phone.Controls;

namespace SAFPodcast.WP8
{
    /// <summary>
    /// Description for RenameView.
    /// </summary>
    public partial class RenameView : PhoneApplicationPage
    {
        /// <summary>
        /// Initializes a new instance of the RenameView class.
        /// </summary>
        public RenameView()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Messenger.Default.Register<bool>(this, "NavigationBackRequest", (b) => 
                DispatcherHelper.RunAsync(NavigationService.GoBack));
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            Messenger.Default.Unregister<bool>(this, "NavigationBackRequest");
            base.OnNavigatedFrom(e);
        }

        private void abbCancel_Click(object sender, EventArgs e)
        {
            btnCancel.Command.Execute(null);
        }

        private void abbSave_Click(object sender, EventArgs e)
        {
            btnRename.Command.Execute(null);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null)
            {
                var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
                if (bindingExpression != null)
                {
                    bindingExpression.UpdateSource();
                }
            }
        }
    }
}
